const Router = require('koa-router');
const router = new Router();
const tool = require("../../tools/tool");
const {db, Op, QueryTypes} = require("../../models/db");
// 导出票据
router.post('/bill/exportBill', async ctx => {
    // 审核
    let tenantId = ctx.user.tenantId
    let id = ctx.request.body.id
    let model = await db.query(`select id,buName,transType from v_main where tenantId=${tenantId} and id=${id}`,{type: QueryTypes.SELECT, plain: true})
    if(!model) return ctx.err('没找到票据')
    switch (model.transType) {
        case 150601: {
            let detail = await db.query(`select * from v_main_info where tenantId=${tenantId} and iid=${id} `,{type: QueryTypes.SELECT})
            detail.map(row=>{
                row.spec = '1*'+row.spec
            })
            let columns = [
                {field: 'invName', title: '商品名称',excelOut: true},
                {field: 'spec', title: '规格',excelOut: true},
                {field: 'qty', title: '数量',excelOut: true},
                {field: 'unit', title: '单位',excelOut: true},
                {field: 'price', title: '单价',excelOut: true},
                {field: 'deduction', title: '返利',excelOut: true},
                {field: 'amount', title: '金额',excelOut: true},
                {field: 'send', title: '赠品',excelOut: true},
                {field: 'remark', title: '备注',excelOut: true},
                {field: 'barCode1', title: '条码',excelOut: true},
            ]
            return tool.doExcelOut(ctx, detail, {}, columns)
        } break
        case 150602: {
            let detail = await db.query(`select * from v_main_info where tenantId=${tenantId} and iid=${id} `,{type: QueryTypes.SELECT})
            detail.map(row=>{
                row.spec = '1*'+row.spec
            })

            let columns = [
                {field: 'invName', title: '商品名称',excelOut: true},
                {field: 'spec', title: '规格',excelOut: true},
                {field: 'qty', title: '数量',excelOut: true},
                {field: 'unit', title: '单位',excelOut: true},
                {field: 'price', title: '单价',excelOut: true},
                {field: 'amount', title: '金额',excelOut: true},
                {field: 'remark', title: '备注',excelOut: true},
                {field: 'barCode1', title: '条码',excelOut: true},
            ]
            return tool.doExcelOut(ctx, detail, {}, columns)
        } break
        case 150603: {
            let detail = await db.query(`select * from v_main_info where tenantId=${tenantId} and iid=${id} `,{type: QueryTypes.SELECT})
            detail.map(row=>{
                row.spec = '1*'+row.spec
            })

            let columns = [
                {field: 'trans', title: '方向',excelOut: true},
                {field: 'invName', title: '商品名称',excelOut: true},
                {field: 'spec', title: '规格',excelOut: true},
                {field: 'qty', title: '数量',excelOut: true},
                {field: 'unit', title: '单位',excelOut: true},
                {field: 'price', title: '单价',excelOut: true},
                {field: 'amount', title: '金额',excelOut: true},
                {field: 'remark', title: '备注',excelOut: true},
                {field: 'barCode1', title: '条码',excelOut: true},
            ]
            return tool.doExcelOut(ctx, detail, {}, columns)
        } break
        default:
            return ctx.err('还未支持此类票据导出')
    }

})
module.exports = router;